/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.WareInfoPo;
import com.itttq.gshop.domain.to.IndexStatTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.pbpm.service.WareInfoService;

@RestController
@RequestMapping("wareInfo")
public class WareInfoController extends GenericController {
	private static final long serialVersionUID = -4556967612399512L;
	public static final String CACHE_KEY = "wareInfo";
	@Autowired
	private WareInfoService wareInfoService;
	
	/**
	 * 根据条件获取WareInfoPo对象，不返回查询的数据总数
	 * 
	 * @param wareInfo
	 * @Author 迷蝶
	 * @return List<WareInfoPo>
	 */
	@RequestMapping("wares")
	public List<WareInfoPo> getWares(@RequestBody WareInfoPo wareInfo) {
		return wareInfoService.getWares(wareInfo);
	}
	
	/**
	 * 根据条件获取WareInfoPo对象，并返回查询的数据总数
	 * 
	 * @param wareInfo
	 * @Author 迷蝶
	 * @return ResultPageTo<WareInfoPo>
	 */
	@RequestMapping("list")
    public ResultPageTo<WareInfoPo> getWareInfos(@RequestBody WareInfoPo wareInfo) {
        return wareInfoService.getWareInfos(wareInfo);
    }
	
	/**
	 * 根据ID获取WareInfoPo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return WareInfoPo
	 */
	@RequestMapping("/{id:^[0-9]*$}")
	@Cacheable(value=CACHE_KEY, key="'wareInfo_'+#id")
	public WareInfoPo getWareInfo(@PathVariable("id") int id) {
		return wareInfoService.getWareInfo(id);
	}
	
	/**
	 * 保存WareInfoPo对象，并清除此ID的缓存。<br>
	 * 如果ID存在，则更新对象；ID不存在则插入对象。
	 * 
	 * @param wareInfo
	 * @Author 迷蝶
	 * @return WareInfoPo
	 */
	@RequestMapping("/save")
	@CacheEvict(value=CACHE_KEY, key="'wareInfo_'+#wareInfo.id")
	public Integer saveWareInfo(@RequestBody WareInfoPo wareInfo) {
		int rs = 0;
		if(null == wareInfo) {return rs;}
		if(null == wareInfo.getId()) {
			rs = wareInfoService.addWareInfo(wareInfo);
		} else {
			rs = wareInfoService.updateWareInfo(wareInfo);
		}
		return rs;
	}
	
	/**
	 * 根据ID删除WareInfoPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return WareInfoPo
	 */
	@RequestMapping("/del/{id:^[0-9]*$}")
	@CacheEvict(value=CACHE_KEY, key="'wareInfo_'+#id")
	public Integer delWareInfo(@PathVariable("id") int id) {
		return wareInfoService.delWareInfo(id);
	}

	/**
	 * 获取今天新增商品
	 * 
	 * @Author 迷蝶
	 * @return IndexStatTo
	 */
	@RequestMapping("getWareCountToday")
	public IndexStatTo getWareCountToday() {
		return wareInfoService.getWareCountToday();
	}
}